package com.ruoyi.system.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.BatchCard;
import com.ruoyi.system.util.EwmUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CardMapper;
import com.ruoyi.system.domain.Card;
import com.ruoyi.system.service.ICardService;

import javax.annotation.Resource;

/**
 * 礼品卡Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-20
 */
@Service
public class CardServiceImpl implements ICardService {
    @Autowired
    private EwmUtils ewmUtils;
    private static final Logger log = LoggerFactory.getLogger(TokenService.class);

    @Resource
    private CardMapper cardMapper;

    /**
     * 查询礼品卡
     *
     * @param id 礼品卡主键
     * @return 礼品卡
     */
    @Override
    public Card selectCardById(Long id) {
        return cardMapper.selectCardById(id);
    }

    /**
     * 查询礼品卡列表
     *
     * @param card 礼品卡
     * @return 礼品卡
     */
    @Override
    public List<Card> selectCardList(Card card) {
        return cardMapper.selectCardList(card);
    }

    /**
     * 新增礼品卡
     *
     * @param card 礼品卡
     * @return 结果
     */
    @Override
    public AjaxResult insertCard(Card card) {
        AjaxResult ajaxResult = new AjaxResult();

        Card paramCard = new Card();
        paramCard.setCardNo(card.getCardNo());
        List<Card> cards = cardMapper.selectCardList(paramCard);
        if (cards.size() == 0) {
            // 扫码跳转地址
            String address = "http://m.1banlv.cn?cardNo=" + card.getCardNo();
            // 生成二维码的图片类型
//        String imageType = "PNG";
            // 调用生成二维码方法
//        String ewmInfo = ewmUtils.generateBase64(address, imageType);
            card.setUrl(address);
            card.setCreateTime(DateUtils.getNowDate());
            cardMapper.insertCard(card);
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_SUCCESS);
            ajaxResult.put(AjaxResult.MSG_TAG, "新增成功！");

        }else{
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
            ajaxResult.put(AjaxResult.MSG_TAG, "新增失败，卡号重复请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 修改礼品卡
     *
     * @param card 礼品卡
     * @return 结果
     */
    @Override
    public int updateCard(Card card) {
        card.setUpdateTime(DateUtils.getNowDate());
        return cardMapper.updateCard(card);
    }

    /**
     * 批量删除礼品卡
     *
     * @param ids 需要删除的礼品卡主键
     * @return 结果
     */
    @Override
    public int deleteCardByIds(Long[] ids) {
        return cardMapper.deleteCardByIds(ids);
    }

    /**
     * 删除礼品卡信息
     *
     * @param id 礼品卡主键
     * @return 结果
     */
    @Override
    public int deleteCardById(Long id) {
        return cardMapper.deleteCardById(id);
    }

    /**
     * 新增礼品卡
     *
     * @param batchCard 礼品卡
     * @return 结果
     */
    public AjaxResult insertBatchCard(BatchCard batchCard) {
        AjaxResult ajaxResult = new AjaxResult();
        Integer success = 0;
        Integer fail = 0;
        try {
            Random random = new Random();
            for (int i = 0; i < batchCard.getNum(); i++) {
                //卡号
                int number = random.nextInt(10000000);
                String numberString = String.format("%07d", number);
                //密码
                int number2 = random.nextInt(10000000);
                String password = String.format("%07d", number2);
                Set<Integer> set = new HashSet<>();
                set.add(number);
                while (set.contains(number)) {
                    number = random.nextInt(10000000);
                    numberString = String.format("%07d", number);
                }

                set.add(number);
                Card param = new Card();
                param.setCardNo(numberString);
                List<Card> cardList = cardMapper.selectCardList(param);
                if (cardList.size() > 0) {
                    fail++;
                } else {
                    Card creCard = new Card();
                    creCard.setCardNo(numberString);
                    creCard.setCardSetId(batchCard.getCardSetId());
                    creCard.setPassword(password);
                    // 扫码跳转地址
                    String address = "http://m.1banlv.cn?cardNo=" + numberString;
                    // 生成二维码的图片类型
//                    String imageType = "PNG";
//                    // 调用生成二维码方法
//                    String ewmInfo = ewmUtils.generateBase64(address, imageType);
                    creCard.setUrl(address);
                    System.err.println("二维码跳转路由：" + address);
                    cardMapper.insertCard(creCard);
                    success++;
                }
            }


            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_SUCCESS);
            ajaxResult.put(AjaxResult.MSG_TAG, "成功" + success + "个，失败" + fail + "个，请重新生成失败的卡片数量！");
        } catch (Exception e) {
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
            ajaxResult.put(AjaxResult.MSG_TAG, "生成失败，请联系管理员！");
            log.error("生成礼品卡异常：", e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

    public void ewmCard(String cardNo) {
        // 扫码跳转地址
        String address = "http://m.1banlv.cn?cardNo=" + cardNo;
        // 生成二维码的图片类型
        String imageType = "PNG";
        // 调用生成二维码方法
        String ewmInfo = ewmUtils.generateBase64(address, imageType);
        System.err.println("二维码内容：" + ewmInfo);
    }

    public void createCardNo(BatchCard batchCard) {
        Random random = new Random();
        for (int i = 0; i < batchCard.getNum(); i++) {
            //卡号
            int number = random.nextInt(10000000);
            String numberString = String.format("%07d", number);
            //密码
            int number2 = random.nextInt(10000000);
            String password = String.format("%07d", number2);
            Set<Integer> set = new HashSet<>();
            set.add(number);
            while (set.contains(number)) {
                number = random.nextInt(10000000);
                numberString = String.format("%07d", number);
            }
            set.add(number);
            Card param = new Card();
            param.setCardNo(numberString);
            List<Card> cardList = cardMapper.selectCardList(param);
            if (cardList.size() > 0) {
            } else {
                Card creCard = new Card();
                creCard.setCardNo(numberString);
                creCard.setCardSetId(batchCard.getCardSetId());
                creCard.setPassword(password);
                // 扫码跳转地址
                String address = "http://m.1banlv.cn?cardNo=" + numberString;
                // 生成二维码的图片类型
                String imageType = "PNG";
                // 调用生成二维码方法
                String ewmInfo = ewmUtils.generateBase64(address, imageType);
                creCard.setUrl(ewmInfo);
                System.err.println("二维码跳转路由：" + address);
                cardMapper.insertCard(creCard);
            }
        }

    }

    public void resetting(){
        cardMapper.resetting();
    }



}
